<?php

/**
 * Extends the default Quote_Item to integrate some fields
 * @author nicple
 *
 */
class  Orthographe_WordCounter_Model_Quote_Item extends Mage_Sales_Model_Quote_Item {


	/**
	 * Delay Message to display
	 * @var string
	 */
	private $_delayMessage = NULL;

	/**
	 * Initial Delay in Hours
	 * @var integer
	 */
	private $_initialDelay;

	/**
	 * Final Customer Delay in Hours
	 * @var integer
	 */
	private $_finalDelay;

	/**
	 * Seuil of the Quote Item
	 * @var integer
	 */
	private $_seuil;


	/// Accesseurs
	/**
	 * Get Delay Message to display
	 * @return string
	 */
	public function getDelayMessage(){

		return Mage::helper('orthographewordcounter')->getDelayMessage($this);
	}

	/**
	 * Return the seuil of the quote Item
	 * @return number
	 */
	public function getSeuil(){
		if(!isset($this->_seuil))
			return 0;

		return $this->_seuil;
	}


	/**
	 * Set the seuil of the Quote Item
	 * @param integer $seuilToput
	 */
	public function setSeuil($seuilToput){

		Mage::log("NPL - ".get_class($this)." -Seuil to save = ".$seuilToput,Zend_Log::DEBUG);
		if(!isset($seuilToput)){
			$this->_seuil=0;
		}
		$this->_seuil= $seuilToput;
	}

	/**
	 * Return the Initial Delay in hours
	 * @return number
	 */
	public function getInitialDelay(){
		if(!isset($this->_initialDelay))
			return 0;

		return $this->_initialDelay;
	}

	/**
	 * Set the Initial Delay in hours
	 * @param integer $delay
	 */
	public function setInitialDelay($delay){

		Mage::log("NPL - ".get_class($this)." -Initial delay to save = ".$delay,Zend_Log::DEBUG);
		if(!isset($delay) || !is_int($delay)){
			$this->_initialDelay=0;
		}
		$this->_initialDelay= $delay;
	}


	/**
	 * Return the Final Delay in hours
	 * @return number
	 */
	public function getFinalDelay(){
		if(!isset($this->_finalDelay))
			return 0;

		return $this->_finalDelay;
	}

	/**
	 * Set the Final Delay in hours
	 * @param integer $delay
	 */
	public function setFinalDelay($delay){

		Mage::log("NPL - ".get_class($this)." -Final delay to save = ".$delay,Zend_Log::DEBUG);
		if(!isset($delay) || !is_int($delay)){
			$this->_finalDelay=0;
		}
		$this->_finalDelay= $delay;
	}


	function __construct() {
		parent::__construct();
// 		$this->_init('wordcounter/wordcounter');
	}

	/**
	 * Overide Save model plus its options
	 * Ensures saving options in case when resource model was not changed
	 */
	public function save()
	{
		// 		$hasDataChanges = $this->hasDataChanges();
		// 		$this->_flagOptionsSaved = false;
// 		Mage::log("NPL - ".get_class($this)." - Try to save Ajuste Quote Item : ".$this->hasDataChanges(),Zend_Log::DEBUG);
		try {
// 			$data = array(
// 					"initial_delay" => $this->_initialDelay,
// 					"final_delay" => $this->_finalDelay,
// 					"seuil" =>$this->_seuil
// 			);
			
// 			$data = array(
// 					"initial_delay" => 2,
// 					"final_delay" => 48,
// 					"seuil" => 46
// 			);
			

// 			$this->_initialDelay = 2;
			
			
// 			Mage::log("NPL - ".get_class($this)." -Quote ITEM to be saved BEFORE UPDATE = ".print_r($data,true),Zend_Log::DEBUG);
			
// 			//$this->setData($data);
						
// 			Mage::log("NPL - ".get_class($this)." -Quote ITEM to be saved BEFORE UPDATE Message = ".$this->getDelayMessage());
			
// // 			Mage::log("NPL - ".get_class($this)." -Quote ITEM to be saved = ".print_r($this,true),Zend_Log::DEBUG);
			
// // 			Mage::log("NPL - ".get_class($this)." - Before Parent saving",Zend_Log::DEBUG);
// // 		//NPL !!	
		    parent::save();
			
// 			Mage::log("NPL - ".get_class($this)." - Successfully saved Ajuste Quote Item",Zend_Log::INFO);
			
		}catch(Exception $e) {
			Mage::log("NPL - ".get_class($this)." - Getting Custom Option details is not possible !".$e->getMessage(),Zend_Log::ERR);
		}



	}

	/**
	 * Clone overide quote item
	 *
	 * @return Orthographe_WordCounter_Model_Quote_Item
	 */
	public function __clone()
	{
		parent::__clone();
		$this->_delayMessage    = null;
		$this->_seuil = 0;
		$this->_finalCustomerDelay = 0;
		$this->_initialDelay = 0;

		return $this;
	}



}